package com.property.cleaning.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.property.cleaning.entity.Area;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 区域Mapper接口
 * 
 * @author Property Cleaning System
 */
@Mapper
public interface AreaMapper extends BaseMapper<Area> {

    /**
     * 获取区域树形结构
     */
    @Select("SELECT a.*, u.real_name as manager_name FROM areas a " +
            "LEFT JOIN users u ON a.manager_id = u.id " +
            "WHERE a.status = 1 ORDER BY a.area_path")
    List<Area> findAreaTree();

    /**
     * 根据父ID获取子区域
     */
    @Select("SELECT a.*, u.real_name as manager_name FROM areas a " +
            "LEFT JOIN users u ON a.manager_id = u.id " +
            "WHERE a.parent_id = #{parentId} AND a.status = 1 ORDER BY a.area_name")
    List<Area> findByParentId(@Param("parentId") Long parentId);

    /**
     * 根据区域编码查询
     */
    @Select("SELECT * FROM areas WHERE area_code = #{areaCode} AND status = 1")
    Area findByAreaCode(@Param("areaCode") String areaCode);

    /**
     * 获取所有顶级区域
     */
    @Select("SELECT a.*, u.real_name as manager_name FROM areas a " +
            "LEFT JOIN users u ON a.manager_id = u.id " +
            "WHERE a.parent_id = 0 AND a.status = 1 ORDER BY a.area_name")
    List<Area> findRootAreas();
}